package com.nbufe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbufe.common.constant.QueryPageParam;
import com.nbufe.common.constant.Result;
import com.nbufe.entity.CmsData;
import com.nbufe.entity.CmsLog;
import com.nbufe.service.CmsDataService;
import com.nbufe.service.CmsLogService;
import com.nbufe.vo.AddVo;
import com.nbufe.vo.DataVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ylz.java
 * @since 2023-03-22
 */
@RestController
@RequestMapping("/ylz/cms-data")
public class CmsDataController {


    @Autowired
    private CmsDataService cmsDataService;
    @Autowired
    private CmsLogService cmsLogService;

    @PostMapping("/select")
    @ApiOperation("查询所有的设备数据(无权限)")
    public Result list123(@RequestBody AddVo addVo) {
        QueryWrapper<CmsData> wrapperChapter = new QueryWrapper<>();
        String deviceid = addVo.getDeviceId();
        String valuetype = addVo.getValueType();
        Date starttime = addVo.getStartTime();
        Date endtime = addVo.getEndTime();
        if (StringUtils.isNotBlank(deviceid) && !"null".equals(deviceid)) {
            wrapperChapter.like("device_id", deviceid);
        }
        if (StringUtils.isNotBlank(valuetype) && !"null".equals(valuetype)) {
            wrapperChapter.like("value_type", valuetype);
        }
        if (StringUtils.checkValNotNull(starttime)) {
            // >=
            wrapperChapter.ge("createtime", starttime);
        }
        if (StringUtils.checkValNotNull(endtime)) {
            // <=
            wrapperChapter.le("createtime", endtime);
        }
        List<CmsData> list = cmsDataService.list(wrapperChapter);
        cmsLogService.save(new CmsLog("管理员查询了所有设备数据"));
        return list.size() > 0 ? Result.suc(list) : Result.fail();
    }

    @PostMapping("/cms-data/shidu")
    @ApiOperation("返回第一个湿度")
    public Result listshidu(@RequestBody DataVo dataVo) {
        QueryWrapper<CmsData> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("device_id", dataVo.getDeviceId());
        wrapperChapter.eq("value_type", dataVo.getValueType());
        wrapperChapter.orderByDesc("createTime");
        wrapperChapter.last("limit 1");
        CmsData one = cmsDataService.getOne(wrapperChapter);
        if (one == null) {
            return Result.fail();
        }
        return Result.suc(one.getValue());
    }


    @GetMapping("/cms-data/list")
    @ApiOperation("查询所有的设备数据")
    public Result list() {
        cmsLogService.save(new CmsLog("管理员查询了所有设备数据"));
        return Result.suc(cmsDataService.list(null));
    }

    @GetMapping("/cms-data/id")
    @ApiOperation("根据id查询数据")
    public Result findById(@RequestParam String id) {
        cmsLogService.save(new CmsLog("管理员查询了id为" + id + "的设备数据"));
        return Result.suc(cmsDataService.getById(id));
    }


    @GetMapping("/cms-data/findByDeviceid")
    @ApiOperation("找数据库中是否存在该设备id的数据")
    public Result findByDeviceid(@RequestParam String deviceid) {
        QueryWrapper<CmsData> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("device_id", deviceid);
        List<CmsData> list = cmsDataService.list(wrapperChapter);
        cmsLogService.save(new CmsLog("管理员查询了id为" + deviceid + "的设备数据"));
        return list.size() > 0 ? Result.suc(list) : Result.fail();
    }

    @PostMapping("/cms-data/save")
    @ApiOperation("新增一条数据")
    public Result save(@RequestBody CmsData cmsData) {
        cmsLogService.save(new CmsLog("管理员新增了一条设备数据"));
        return cmsDataService.save(cmsData) ? Result.suc() : Result.fail();
    }


    @PostMapping("/adddata")
    @ApiOperation("新增一条数据")
    public Result adddata(@RequestBody CmsData cmsData) {
        cmsLogService.save(new CmsLog("管理员新增了一条设备数据"));
        return cmsDataService.save(cmsData) ? Result.suc() : Result.fail();
    }


    @PostMapping("/cms-data/update")
    @ApiOperation("根据ID更新一条设备数据")
    public Result update(@RequestBody CmsData cmsData) {
        cmsLogService.save(new CmsLog("管理员更新了id为" + cmsData.getDeviceId() + "的设备数据"));
        return cmsDataService.updateById(cmsData) ? Result.suc() : Result.fail();
    }

    @ApiOperation("删除一条设备数据")
    @GetMapping("/cms-data/del")
    public Result del(@RequestParam String id) {
        cmsLogService.save(new CmsLog("管理员删除了id为" + id + "的设备数据"));
        return cmsDataService.removeById(id) ? Result.suc() : Result.fail();
    }


    @PostMapping("/cms-data/listPage")
    @ApiOperation("分页模糊查询")
    public Result listPage(@RequestBody QueryPageParam query) {


        HashMap param = query.getParam();
        String deviceid = (String) param.get("deviceid");

        //当前页、每页多少条
        Page<CmsData> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        QueryWrapper<CmsData> wrapperChapter = new QueryWrapper<>();
        if (StringUtils.isNotBlank(deviceid) && !"null".equals(deviceid)) {
            wrapperChapter.like("device_id", deviceid);
        }

        wrapperChapter.orderByDesc("createTime");

        IPage result = cmsDataService.page(page, wrapperChapter);
        cmsLogService.save(new CmsLog("管理员分页查询了设备数据"));
        return Result.suc(result.getRecords(), result.getTotal());
    }
}

